package org.example.app;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

public class Server {

    private static CopyOnWriteArrayList<Pip> list = new CopyOnWriteArrayList<Pip>();
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10000);

        while (true){
           Socket socket = ss.accept();
           System.out.println("一个用户连接");
           Pip pip = new Pip(socket);
           list.add(pip);
           new Thread(pip).start();
        }
    }

    static class Pip implements  Runnable{

        private  boolean runing = true;
        private  Socket socket;
        private DataInputStream dis;
        private DataOutputStream dos;
        private  String name;
        Pip(Socket s)  {
            this.socket = s;
            try {
                dis = new DataInputStream(s.getInputStream());
                dos = new DataOutputStream(s.getOutputStream());
                this.name = receive();
                this.send("欢迎你的到来");
                sendothers(this.name+"来到了聊天室",true);//系统消息
            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        }

        /**
         * 接收数据
         * @return
         */
        private String receive(){
            String msg = "";
            try {
               msg =  dis.readUTF();
            } catch (IOException e) {
                release();
                throw new RuntimeException(e.getMessage());
            }
            return msg;
        }

        /**
         * 发送数据
         * @param msg
         */
        private void send(String msg){
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        //释放资源
        private void release() {
            try {
                if( dos != null )
                    dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                if( dis != null )
                    dis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                if( socket != null )
                    socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see Thread#run()
         */
        public void run() {
            while( runing ) {
                //收到请求
                String msg  = receive();

                //反馈结果
                if( !msg.equals(""))
                    sendothers(msg,false);
                else
                    runing = false;
            }
            //释放资源
            release();
        }

        private void sendothers(String msg, boolean isSys) {
            for (Pip item:list){
                if (item==this){
                    continue;
                }
                if(!isSys)//群消息
                    item.send(this.name+"说："+msg);
                else//系统消息
                    item.send(msg);
            }
        }
    }
}
